
package com.bootx.security;

import com.bootx.entity.Admin;
import org.jetbrains.annotations.NotNull;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * Security - 当前用户MethodArgumentResolver
 *
 * @author 好源++ Team
 * @version 6.1
 */
public class CurrentUserMethodArgumentResolver implements HandlerMethodArgumentResolver {


	/**
	 * 支持参数
	 *
	 * @param methodParameter
	 *            MethodParameter
	 * @return 是否支持参数
	 */
	@Override
	public boolean supportsParameter(MethodParameter methodParameter) {
		return methodParameter.hasParameterAnnotation(CurrentUser.class)  && Admin.class.isAssignableFrom(methodParameter.getParameterType());
	}

	/**
	 * 解析变量
	 *
	 * @param methodParameter
	 *            MethodParameter
	 * @param modelAndViewContainer
	 *            ModelAndViewContainer
	 * @param nativeWebRequest
	 *            NativeWebRequest
	 * @param webDataBinderFactory
	 *            WebDataBinderFactory
	 * @return 变量
	 */
	@Override
	public Object resolveArgument(@NotNull MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, @NotNull NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory){
		Admin admin = new Admin();
		String adminId = nativeWebRequest.getParameter("adminId");
		try {
            assert adminId != null;
            admin.setId(Long.valueOf(adminId));
		}catch (Exception ignored){
		}
		return admin;
	}

}